<%= render :partial => "/tr8n/admin/common/header" %>
<%= render :partial => "tabs" %>
<%= tr8n_will_filter(@keys) %>


<%= tr8n_actions_tag([
        link_to_function("Add Translation Key", "Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_update')"),
        link_to_function("Update Translation Counts", 'updateTranslationCounts()')
      ])
%>
<br>

<%= form_tag("", :id => "tr8n_form") do %>
  <%= hidden_field_tag(:type, "translation_key") %>
	<%= will_filter_table_tag(@keys, :columns => [
    [:icn, image_tag("tr8n/icons/key.png"), :title => "", :style => "width:12px;"],
	  [:checkbox, :keys],
    :id,
	  [:label, lambda{|key|
	    html = [link_to(key.label, :action => :view, :id => key.id)]
	    html << '<div style="color:grey; font-size:10px;">'
	    html << key.description.to_s  
	    html << '</div>'  
      html.join("").html_safe
	  }],
    [:locale, :filterable => true],
    [:admin, lambda{|key| key.admin}, :style => 'padding:5px;text-align:right;'],
    [:level, lambda{|key| key.level},  :style => 'padding:5px;text-align:right;'], 
	  [:translation_count, lambda{|key| link_to(key.translation_count || 0, "/tr8n/admin/translation/index?wf_c0=translation_id&wf_o0=is&wf_v0_0=#{key.id}")}, 'padding:5px;text-align:right;'],
	  [:created_at, lambda{|key| key.created_at}, 'padding:5px;text-align:right;'],
	  [:verified_at, lambda{|key| key.verified_at}, 'padding:5px;text-align:right;'],
	  [:actions, lambda{|key|
      tr8n_actions_tag([
	      link_to_function("Update", "Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_update?id=#{key.id}')"),
        link_to_function("Delete", "deleteObjects('#{key.id}')")
      ])
	  }, 'padding:5px;text-align:right;white-space:nowrap;']
	]) %>
<% end %>

<%= will_filter_actions_bar_tag(@keys, [
  ['Merge Selected Keys', 'mergeSelectedKeys()'],
  ['Delete Selected Keys', 'deleteObjects()'],
  ['Associate With Source', 'addSelectedKeysToSource()'],
]) %>

<%= render :partial => "/tr8n/admin/common/footer" %>

<script>
  function getSelectedKeyIds() {
    var i=0;
    var checkbox = Tr8n.element('wf_check_' + i);
    var keys = [];
    while (checkbox) {
      if (checkbox.checked) {
        keys.push(checkbox.value);
      }
      checkbox = Tr8n.element('wf_check_' + (++i));
    }
    return keys; 
  }

	function mergeSelectedKeys() {
		var keys = getSelectedKeyIds();
		if (keys.length < 2) {
			alert('At least two keys must be selected.');
			return;
		}
		Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_merge?keys=' + keys, {height:350});
	}

  function updateTranslationCounts() {
    if (!confirm('Are you sure you want to run this opperation?'))
       return;
    location = '/tr8n/admin/translation_key/update_translation_counts' 
  }

  function addSelectedKeysToSource() {
    var keys = getSelectedKeyIds();
    if (keys.length == 0) {
      alert('At least one key must be selected.');
      return;
    }
    Tr8n.UI.Lightbox.show('/tr8n/admin/translation_key/lb_add_to_source?keys=' + keys, {height:350});
  }

</script>